//
//  HWComposeToolbar.m
//  01-黑马微博
//
//  Created by lanou3g on 15/9/21.
//  Copyright (c) 2015年 heima. All rights reserved.
//

#import "HWComposeToolbar.h"

@implementation HWComposeToolbar
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"compose_toolbar_background"]];
        [self setupBtnWithImage:[UIImage imageNamed:@"compose_camerabutton_background"] highlightImage:[UIImage imageNamed:@"compose_camerabutton_background_highlighted"] type:HWComposeToolbarCamera];
        [self setupBtnWithImage:[UIImage imageNamed:@"compose_toolbar_picture"] highlightImage:[UIImage imageNamed:@"compose_toolbar_picture_highlighted"] type:HWComposeToolbarPicture];
        [self setupBtnWithImage:[UIImage imageNamed:@"compose_trendbutton_background"] highlightImage:[UIImage imageNamed:@"compose_trendbutton_background_highlighted"] type:HWComposeToolbarMention];
        [self setupBtnWithImage:[UIImage imageNamed:@"compose_mentionbutton_background"] highlightImage:[UIImage imageNamed:@"compose_mentionbutton_background_highlighted"] type:HWComposeToolbarTrend];
        [self setupBtnWithImage:[UIImage imageNamed:@"compose_emoticonbutton_background"] highlightImage:[UIImage imageNamed:@"compose_emoticonbutton_background_highlighted"] type:HWComposeToolbarEmotion];
    }
    return self;
}

- (void)setupBtnWithImage:(UIImage *)image highlightImage:(UIImage *)highImage type:(HWComposeToolbarType)type{
    UIButton *btn = [[UIButton alloc] init];
    [btn setImage:image forState:UIControlStateNormal];
    [btn setImage:highImage forState:UIControlStateHighlighted];
    btn.tag = type;
    [btn addTarget:self action:@selector(btnOnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:btn];
}
- (void)btnOnClick:(UIButton *)sender{
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(composeToolbar:didClickedButton:)]) {
        [self.delegate composeToolbar:self didClickedButton:sender.tag];
    }
}

- (void)layoutSubviews{
    [super layoutSubviews];
    CGFloat btnW = self.width/5;
    CGFloat btnH = self.height;
    NSUInteger count = self.subviews.count;
    for (int i = 0; i< count; i++) {
        UIButton *btn = self.subviews[i];
        btn.x = i*btnW;
        btn.y = 0;
        btn.width = btnW;
        btn.height = btnH;
    }
}
@end
